import { Body, Controller, Get, Post, Request } from '@nestjs/common';
import { UserService } from './user.service';
import { User } from 'src/database/entities/user';
import { SkipToken } from 'src/meta/index';

@Controller('user')
export class UserController {
  constructor(private userServers: UserService) {}

  @Post('/create')
  @SkipToken()
  async create(@Body() user: User) {
    await this.userServers.createUser(user);
    return {
      message: '创建成功',
    };
  }

  @Post('/login')
  @SkipToken()
  async login(@Body() user: User) {
    const token = await this.userServers.loginUser(user);
    return {
      message: '登录成功',
      token,
    };
  }

  @Get('info')
  info() {
    return {
      message: '获取用户信息成功',
      a: 1,
    };
  }

  @Get('/logout')
  async logout(@Request() req: Request) {
    const userId = req.userPayload.id;
    await this.userServers.logout(userId);
    return {
      message: '退出成功',
    };
  }
}
